##### This script generates the data needed to make Table 5.

##################################################
# ANES variable selection
##################################################

library(readstata13)

library(questionr)

d <- read.dta13('../data/anes_timeseries_2012_dta/anes_timeseries_2012.dta', convert.factors=F)

pers <- subset(d, select=c("pid_x", "mode", "weight_full", "weight_ftf", "weight_web", "tipi_extra", "tipi_crit", "tipi_dep", "tipi_anx", "tipi_open", "tipi_resv", "tipi_warm", "tipi_disorg", "tipi_calm", "tipi_conv", "dem_raceeth_x", "gender_respondent_x", "dem_edugroup_x"))

pers[pers<0] <- NA

lapply(pers, function(x) table(x, exclude=F))

##################################################
# VARIABLE CREATION
##################################################

### indicators for PID7=4 and Internet sample

pers$pureind <- ifelse(pers$pid_x==4, 1, 0)

pers$internet <- ifelse(pers$mode==2, 1, 0)

### gender (two-category)

pers$gender <- pers$gender_respondent_x
pers$gender[pers$gender==1] <- "male"
pers$gender[pers$gender==2] <- "female"

### race (four-category)

pers$race <- pers$dem_raceeth_x
pers$race[pers$race==1] <- "white"
pers$race[pers$race==2] <- "black"
pers$race[pers$race==5] <- "hispanic"
pers$race[pers$race %in% c(3,4,6)] <- "other"
pers$race <- factor(pers$race, levels=c("white", "black", "hispanic", "other"))

### education (four- or five-category)

pers$edu <- pers$dem_edugroup_x
pers$edu[pers$edu==1] <- "SomeHS"
pers$edu[pers$edu==2] <- "HS"
pers$edu[pers$edu==3] <- "SomeCollege"
pers$edu[pers$edu==4] <- "College"
pers$edu[pers$edu==5] <- "GradDegree"
pers$edu <- factor(pers$edu, levels=c('SomeHS', 'HS', 'SomeCollege', 'College', 'GradDegree'))

##################################################
# CROSS-TABULATION
##################################################

### 2012 f2f: pid7 x gender, pid7 x race (white, black, hisp, other), pid7 x education (4 or 5 cat), pid7 x each of the personality types

gender.ftf <- 100*prop.table(wtd.table(x=pers$pid_x[pers$internet==0], y=pers$gender[pers$internet==0], weights=pers$weight_ftf[pers$internet==0]), margin=2) ## gender, FTF sample

write.csv(gender.ftf, '../output/gender_ftf.csv')

gender.web <- 100*prop.table(wtd.table(x=pers$pid_x[pers$internet==1], y=pers$gender[pers$internet==1], weights=pers$weight_web[pers$internet==1]), margin=2) ## gender, web sample

write.csv(gender.web, '../output/gender_web.csv')

race.ftf <- 100*prop.table(wtd.table(x=pers$pid_x[pers$internet==0], y=pers$race[pers$internet==0], weights=pers$weight_ftf[pers$internet==0]), margin=2) ## race, FTF sample 

write.csv(race.ftf, '../output/race_ftf.csv')

race.web <- 100*prop.table(wtd.table(x=pers$pid_x[pers$internet==1], y=pers$race[pers$internet==1], weights=pers$weight_web[pers$internet==1]), margin=2) ## race, web sample 

write.csv(race.web, '../output/race_web.csv')

edu.ftf <- 100*prop.table(wtd.table(x=pers$pid_x[pers$internet==0], y=pers$edu[pers$internet==0], weights=pers$weight_ftf[pers$internet==0]), margin=2) ## education, FTF sample

write.csv(edu.ftf, '../output/edu_ftf.csv')

edu.web <- 100*prop.table(wtd.table(x=pers$pid_x[pers$internet==1], y=pers$edu[pers$internet==1], weights=pers$weight_web[pers$internet==1]), margin=2) ## education, web sample

write.csv(edu.web, '../output/edu_web.csv')